package utilities;

import java.io.Serializable;

/**
 * Timespan class.
 * @author pcdejager
 */
public final class Timespan
    implements Serializable, Cloneable, Comparable<Timespan> {
    /**
     * Default constructor.
     */
    public Timespan() {
        days = 0;
    }
    /**
     * Constructor.
     * @param d The number of days in the span
     */
    public Timespan(final int d) {
        days = d;
    }

    /**
     * Constructor from two dates.
     * @param from The start date
     * @param to The end date
     */
    public Timespan(final WorldDate from, final WorldDate to) {
        days = to.getTicks() - from.getTicks();
    }

    /**
     * Get the total number of days in the span.
     * @return The days
     */
    public int getDays() {
        return days;
    }
    /**
     * Get the total number of years in the span.
     * @return The years
     */
    public int getYears() {
        return days / TimeProperties.getInstance().daysInAYear();
    }

    /**
     * Set the total number of days in the span.
     * @param d The total number of days
     */
    public void setDays(final int d) {
        days = d;
    }

    /**
     * Set the time span.
     * @param from The from date
     * @param to The end date
     */
    public void set(final WorldDate from, final WorldDate to) {
        days = to.getTicks() - from.getTicks();
    }

    /**
     * Clone the object.
     * @return A cloned instance of the class
     */
    public Object clone() {
        return new Timespan(days);
    }

    /**
     * Check if date after specified date.
     * @param when Date to check against
     * @return True if after, else false
     */
    public boolean after(final Timespan when) {
        return days > when.getDays();
    }
    /**
     * Check if date is before specified date.
     * @param when Date to check against
     * @return True if before, else false
     */
    public boolean before(final Timespan when) {
        return days < when.getDays();
    }
    /**
     * Check if dates are equal.
     * @param obj The object to check against
     * @return True if the same, else false
     */
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj.getClass().equals(Timespan.class)) {
            return (days == ((Timespan) obj).getDays());
        }
        return false;
    }

    /**
     * Compare two dates.
     * @param anotherSpan Date to check against
     * @return Negative if before, 0 is equals, positive if after
     */
    public int compareTo(final Timespan anotherSpan) {
        return days - anotherSpan.getDays();
    }

    /**
     * Get hash code.
     * @return The hash code
     */
    public int hashCode() {
        return (int) (days ^ (days >> 32));
    }

    /**
     * Total days.
     */
    private int days = 0;
    /**
     * Serial version.
     */
    private static final long serialVersionUID = 1L;
}
